package com.smarttable.ysd.floatwindow;

import com.smarttable.ysd.R;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;

public class MuteService extends Service {

	LinearLayout mFloatLayout;
	LayoutParams wmParams;
	WindowManager mWindowManager;

	LinearLayout mFloatView;
	Button floatButton1;

	boolean isMoving = false;

	private static final String TAG = "FxService";
	float downX, downY, upX, upY;

	@Override
	public void onCreate() {
		super.onCreate();
		Log.i(TAG, "oncreat");
		createFloatView();
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	private void createFloatView() {
		wmParams = new LayoutParams();
		mWindowManager = (WindowManager) getApplication().getSystemService(getApplication().WINDOW_SERVICE);
		wmParams.type = LayoutParams.TYPE_PHONE;
		wmParams.format = PixelFormat.RGBA_8888;
		wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;
		// LayoutParams.FLAG_NOT_TOUCH_MODAL |
		// LayoutParams.FLAG_NOT_TOUCHABLE

		// wmParams.gravity = Gravity.RIGHT | Gravity.TOP;
		wmParams.gravity = Gravity.RIGHT | Gravity.BOTTOM;

		wmParams.x = 0;
		wmParams.y = 100;

		/*
		 */

		wmParams.windowAnimations = android.R.style.Animation_Translucent;

		wmParams.width = LayoutParams.WRAP_CONTENT;
		wmParams.height = LayoutParams.WRAP_CONTENT;

		LayoutInflater inflater = LayoutInflater.from(getApplication());
		mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_layout_mute,
				null);
		mWindowManager.addView(mFloatLayout, wmParams);

		Log.i(TAG, "mFloatLayout-->left" + mFloatLayout.getLeft());
		Log.i(TAG, "mFloatLayout-->right" + mFloatLayout.getRight());
		Log.i(TAG, "mFloatLayout-->top" + mFloatLayout.getTop());
		Log.i(TAG, "mFloatLayout-->bottom" + mFloatLayout.getBottom());

		mFloatView = (LinearLayout) mFloatLayout.findViewById(R.id.float_id);

		mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
				.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
		Log.i(TAG, "Width/2--->" + mFloatView.getMeasuredWidth() / 2);
		Log.i(TAG, "Height/2--->" + mFloatView.getMeasuredHeight() / 2);

		floatButton1 = (Button) mFloatLayout.findViewById(R.id.fbutton1);
		floatButton1.setTag("1");

		OnTouchListener otl = (new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
					case MotionEvent.ACTION_DOWN:
						isMoving = false;
						downX = event.getRawX();
						downY = event.getRawY();
						break;
					case MotionEvent.ACTION_UP:
						upX = event.getRawX();
						upY = event.getRawY();
						if (!isMoving) {
							Log.i(TAG, v.getTag() + " clicked");
							switch (Integer.parseInt(v.getTag().toString())) {
								case 1:
									setSoundImage();
									toggleRingMode();
									Message m = new Message();
									m.what = 0x123;
									handler.sendMessageDelayed(m, 1000);
									break;
								case 3:
									break;
							}
							return false;
						}
				}
				mWindowManager.updateViewLayout(mFloatLayout, wmParams);
				return false;
			}
		});
		floatButton1.setOnTouchListener(otl);
		mWindowManager.updateViewLayout(mFloatLayout, wmParams);
//		new Thread(new Runnable() {
//
//			@Override
//			public void run() {
//				while (true) {
//					Builder mNotification = new Builder(FxService.this);
//					mNotification.setContentText("").setContentTitle("");
//					mNotification.setWhen(0L).setOngoing(true);
//					ServiceCompatUtils.startForeground(FxService.this, 2,
//							mNotification.build());
//					try {
//						Thread.currentThread().sleep(200);
//					} catch (InterruptedException e) {
//						e.printStackTrace();
//					}
//				}
//			}
//		}).start();
	}

	// ZY add 2015-10-22
	Handler handler = new Handler() {
		@SuppressLint("HandlerLeak")
		public void handleMessage(Message msg) {
			switch (msg.what) {
				case 0x123:
					mWindowManager.updateViewLayout(mFloatLayout, wmParams);
					break;
			}
			super.handleMessage(msg);
		}
	};

	// ZY end
	private String formatStr(Object x) {
		String xx = String.valueOf((int) Float.parseFloat(x.toString()));
		String y = "                                     ";
		return y.substring(0, 10 - xx.length()).concat(xx);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (mFloatLayout != null) {
			mWindowManager.removeView(mFloatLayout);
		}
	}

	private void setSoundImage() {
		AudioManager am = (AudioManager) this.getSystemService(AUDIO_SERVICE);
		boolean mute = am.getStreamVolume(AudioManager.STREAM_MUSIC) == 0;
		int imageResource = mute ? R.drawable.sound_mute : R.drawable.sound_nor;
		this.floatButton1.setBackgroundResource(imageResource);
	}

	private void toggleRingMode() {
		AudioManager am = (AudioManager) this.getSystemService(AUDIO_SERVICE);
		boolean mute = am.getStreamVolume(AudioManager.STREAM_MUSIC) == 0;
		if(mute){
			am.setStreamMute(AudioManager.STREAM_MUSIC, false);
			am.setStreamMute(AudioManager.STREAM_ALARM, false);
			am.setStreamMute(AudioManager.STREAM_DTMF, false);
			am.setStreamMute(AudioManager.STREAM_RING, false);
			am.setStreamMute(AudioManager.STREAM_SYSTEM, false);
			am.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
			am.setStreamMute(AudioManager.STREAM_VOICE_CALL, false);
//			am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

		}else{
			am.setStreamMute(AudioManager.STREAM_MUSIC, true);
			am.setStreamMute(AudioManager.STREAM_ALARM, true);
			am.setStreamMute(AudioManager.STREAM_DTMF, true);
			am.setStreamMute(AudioManager.STREAM_RING, true);
			am.setStreamMute(AudioManager.STREAM_SYSTEM, true);
			am.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
			am.setStreamMute(AudioManager.STREAM_VOICE_CALL, true);
//			am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
		}
	}
}
